package com.example.fw;

import java.util.List;
import java.util.Random;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;

import com.example.tests.GroupObject;

public class HelperWithWebDriverBase {
	protected final AppManager manager;
	protected WebDriver driver;
	
	public HelperWithWebDriverBase(AppManager manager) {
		this.manager = manager;
		driver = manager.getWebDriverHelper().getDriver();
		
	}
	protected void type(By locator, String text) {
		driver.findElement(locator).clear();
		driver.findElement(locator).sendKeys(text);
	}
	
    protected void select(By locator, String optionText) {
        new Select(driver.findElement(locator)).selectByVisibleText(optionText);
    }
    
    protected void selectRandom(By locator) {
        Select select = new Select(driver.findElement(locator));
        List<WebElement> options = select.getOptions();
        if (options.size() > 0){
        int index = new Random().nextInt(options.size());
        options.get(index).click();
        }
    }
    
	protected WebElement findElement(By locator) {
		return driver.findElement(locator);
	}
	protected List<WebElement> findElements(By locator) {
		return driver.findElements(locator);
	}
	
	protected boolean isElementPresent(By locator) {
		try {
			findElement(locator);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
	protected void openUrl(String string) {
		driver.get(string);
	}
	protected void click(By locator) {
		findElement(locator).click();
	}
	
}
